package communicationMessages;

import javax.management.RuntimeErrorException;

import data.User;

public abstract class Message {
	
	protected String content;
	protected User user;
	
	
	public static Message parseMsg (String msg){
		int endIndex = msg.indexOf('\n');
		String title = msg.substring(0, endIndex);
		Message ans = getMessageType(title);
		ans.setContent(msg.substring(endIndex+1));
		return ans;
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	private static Message getMessageType(String title) {
		String register ="register",login = "login",viewForum = "viewForum",viewSubForum ="viewSubForum",viewThread = "viewThread",
				logout = "logout",addThread = "addThread",addPost = "addPost";
		if (title.equals(register))
			return new RegisterMessage();
		else if (title.equals(login))
			return new LoginMessage();
		else if (title.equals(viewForum))
			return new ViewForumMessage();
		else if (title.equals(viewSubForum))
			return new ViewSubForumMessage();
		else if (title.equals(viewThread))
			return new ViewThreadMessage();
		else if (title.equals(logout))
			return new LogoutMessage();
		else if (title.equals(addThread))
			return new AddThreadMessage();
		else if (title.equals(addPost))
			return new AddPostMessage();
		throw new RuntimeException("unknow title to message\n" + title + "\n");
	}

	public abstract String getAnswer();
	
	public static String addFinshToAns(String msg)
	{
		return msg + "\0";
	}

}
